/**
 * Created by IntelliJ IDEA.
 * User: mstine
 * Date: Mar 20, 2008
 * Time: 3:44:33 PM
 * To change this template use File | Settings | File Templates.
 */
use(RandomHelper) {
    15.times{println 10.rand()}
}
println()

class RandomHelper {
    //methods must all accept an instance of themselves (self) as the first argument
    static int rand(Integer self) {
        def r = new Random()
        return r.nextInt(self.intValue())
    }
}

//Slightly more advanced example

use (InternetUtils) {
    println "http://www.google.com/".get()
    println "http://search.yahoo.com/search".get("p=groovy")

    def params = [:]
    params.n = "10"
    params.vl = "lang_eng"
    params.p = "groovy"
    println "http://search.yahoo.com/search".get(params)
}

class InternetUtils {

    static String get(String self) {
        return self.toURL().text
    }

    static String get(String self, String queryString) {
        def url = self + "?" + queryString
        return url.get()
    }

    static String get(String self, Map params) {
        def list = []
        params.each{k,v->
            list << "$k=" + URLEncoder.encode(v)
        }
        def url = self + "?" + list.join("&")
        return url.get()
    }
}

//GOTO: expandoMetaClass.groovy
